package com.nativetalk.controller.app.two.teacher.live;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.live.TdNtLiveCourseHisUpdate;
import com.nativetalk.bean.live.TdNtLiveCourseInsert;
import com.nativetalk.bean.live.TdNtLivePictureDelete;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.controller.app.article.ArticleController;
import com.nativetalk.service.live.insert.LiveInsertService;
import com.nativetalk.service.live.search.LiveSearchService;
import com.nativetalk.service.live.update.LiveUpdateService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class TeacherLiveController {
    private static Logger logger = Logger.getLogger(TeacherLiveController.class);

    @Resource
    private LiveInsertService liveInsertService;
    @Resource
    private LiveSearchService liveSearchService;
    @Resource
    private LiveUpdateService liveUpdateService;
    /**
     * @return RetInfo
     * @Purpose 添加直播
     * @version 2.1
     * @time    2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/his/add", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo LiveHisAdd(@RequestBody Map<String, Object> map,
                                  @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo =  liveInsertService.addLiveHis(Integer.valueOf(map.get("live_cou_id").toString()),
                    new Timestamp(Long.valueOf(map.get("live_cou_his_time").toString())));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 添加图片
     * @version 2.1
     * @time    2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/picture/add", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo LivePictureAdd(@RequestBody Map<String, Integer> map,
                                     @RequestParam("picture") CommonsMultipartFile[] pictures,
                        @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo = liveInsertService.addLiveCoursePicture(map.get("live_cou_id"),tdNtTeacherResult.getTea_id(),pictures);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 删除图片
     * @version 2.1
     * @time    2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/picture/delete", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo LivePictureDelete(@RequestBody TdNtLivePictureDelete tdNtLivePictureDelete,
                                     @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtLivePictureDelete));

            RetInfo retInfo;
            retInfo = liveUpdateService.deleteLivePicture(tdNtLivePictureDelete);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 查找老师直播课程列表
     * @version 2.1
     * @time 2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo list(@RequestBody Page page,
                           @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo = liveSearchService.findLiveByState(tdNtTeacherResult.getTea_id(),page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 查找老师直播课程图片列表
     * @version 2.1
     * @time    2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/pictures", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo pictures(@RequestBody Map<String,Integer> map) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            retInfo = liveSearchService.findLivePictures(map.get("live_cou_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose  添加直播课程
     * @version 2.1
     * @time 2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/add", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo add(@Valid @ModelAttribute TdNtLiveCourseInsert tdNtLiveCourseInsert, BindingResult result,
                           @RequestParam("picture") CommonsMultipartFile[] pictures,
                           @RequestParam("cover_picture") CommonsMultipartFile cover,
                           @RequestHeader(value = "token", required = false) String token) {

        RetInfo retInfo = new RetInfo();
        if (result.hasErrors())   {
            List<FieldError> err=result.getFieldErrors();
            FieldError    fe = err.get(0);
            retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
            retInfo.setTip(fe.getDefaultMessage());
        }else {
            try {
                //打印入参
                logger.info(Constants.prefix_in + JsonUtil.toJson(pictures) +", " +JsonUtil.toJson(cover));

                if (!cover.isEmpty()) {
                    TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
                    tdNtLiveCourseInsert.setTea_id(tdNtTeacherResult.getTea_id());
                    retInfo =  liveInsertService.addLiveCourse(tdNtLiveCourseInsert, pictures, cover);

                    //打印出参
                    logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                    return retInfo;
                } else {
                    retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERNO"));
                    retInfo.setTip("Please add your cover picture");
                }


            } catch (Exception e) {
                //记录错误日志
                retInfo = RetInfo.getServerErrorInfo();
                logger.error(e.getMessage(), e);

                //打印出参
                logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

        }
        return retInfo;
    }
    /**
     * @return RetInfo
     * @Purpose  更新直播课程
     * @version 2.1
     * @time 2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo update(@Valid @ModelAttribute TdNtLiveCourseInsert tdNtLiveCourseInsert, BindingResult result,
                       @RequestParam("cover_picture") CommonsMultipartFile cover) {

        RetInfo retInfo = new RetInfo();
        if (result.hasErrors())   {
            List<FieldError> err=result.getFieldErrors();
            FieldError    fe = err.get(0);
            retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
            retInfo.setTip(fe.getDefaultMessage());
        }else {

            try {
                //打印入参
                logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtLiveCourseInsert) +", " +JsonUtil.toJson(cover));

                retInfo =  liveUpdateService.updateLive(tdNtLiveCourseInsert, cover);

                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;

            } catch (Exception e) {
                //记录错误日志
                retInfo = RetInfo.getServerErrorInfo();
                logger.error(e.getMessage(), e);

                //打印出参
                logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }
        }
        return retInfo;
    }
    /**
     * @return RetInfo
     * @Purpose 查找老师直播课程列表
     * @version 2.1
     * @time 2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/his/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo hisList(@RequestBody Page page,
                        @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            page.setObj(tdNtTeacherResult.getTea_id());
            retInfo = liveSearchService.findLiveHisByTeaId(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 查找老师直播课程列表
     * @version 2.1
     * @time 2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public RetInfo list() {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            Page page = new Page();
            page.setObj(303);
            retInfo = liveSearchService.findLiveHisByTeaId(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
    /**
     * @return RetInfo
     * @Purpose  开始直播
     * @version 2.1
     * @time 2016-05-22
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/his/start", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo start(@RequestBody TdNtLiveCourseHisUpdate tdNtLiveCourseHisUpdate) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtLiveCourseHisUpdate));

            RetInfo retInfo;
            retInfo = liveUpdateService.start(tdNtLiveCourseHisUpdate);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * 废除，从版本6开始不再使用该接口
     * @return RetInfo
     * @Purpose  结束直播
     * @version 2.1
     * @time 2016-05-22
     * @author lizhun
     */
    @RequestMapping(value = "/app/5/teatoken/teacher/live/his/end", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo end(@RequestBody TdNtLiveCourseHisUpdate tdNtLiveCourseHisUpdate) {
        return liveUpdateService.end(tdNtLiveCourseHisUpdate);
    }

}


